package dao;

import pojo.Cart;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDAO {

    private static final String TABLE_NAME = "tb_cart";
    // 根据用户ID查询购物车项
    public static List<Cart> getCartItemsByUserId(int userId) throws SQLException {
        List<Cart> cartItems = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE user_id = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, userId);
        while (resultSet.next()) {
            Cart cart = createCartFromResultSet(resultSet);
            cartItems.add(cart);
        }
        return cartItems;
    }

    // 删除购物车项
    public static void deleteCartItem(int cartId) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE cart_id = ?";
        JDBCUtils.execute(sql, cartId);
    }
    // 检查购物车中是否存在相同商品
    public static Cart getCartItemByUserIdAndProductId(int userId, int productId) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE user_id = ? AND product_id = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, userId, productId);
        if (resultSet.next()) {
            return createCartFromResultSet(resultSet);
        }
        return null;
    }

    // 更新购物车项数量
    public static void updateCartItemQuantity(int cartId, int quantity) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET quantity = ? WHERE cart_id = ?";
        JDBCUtils.execute(sql, quantity, cartId);
    }

    // 添加购物车项
    public static void addCartItem(Cart cart) throws SQLException {
        Cart existingCart = getCartItemByUserIdAndProductId(cart.getUserId(), cart.getProductId());
        if (existingCart != null) {
            // 如果商品已存在，更新数量
            updateCartItemQuantity(existingCart.getCartId(), existingCart.getQuantity() + cart.getQuantity());
        } else {
            // 如果商品不存在，添加新项
            String sql = "INSERT INTO " + TABLE_NAME + " (user_id, product_id, quantity, img) VALUES (?, ?, ?, ?)";
            JDBCUtils.execute(sql, cart.getUserId(), cart.getProductId(), cart.getQuantity(), cart.getImg());
        }
    }
    // 获取所有购物车项
    public static List<Cart> getAllCartItems() throws SQLException {
        List<Cart> cartItems = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        while (resultSet.next()) {
            Cart cart = createCartFromResultSet(resultSet);
            cartItems.add(cart);
        }
        return cartItems;
    }

    // 创建Cart对象从ResultSet
    private static Cart createCartFromResultSet(ResultSet resultSet) throws SQLException {
        Integer cartId = resultSet.getInt("cart_id");
        Integer userId = resultSet.getInt("user_id");
        Integer productId = resultSet.getInt("product_id");
        Integer quantity = resultSet.getInt("quantity");
        String img = resultSet.getString("img");

        Cart cart = new Cart();
        cart.setCartId(cartId);
        cart.setUserId(userId);
        cart.setProductId(productId);
        cart.setQuantity(quantity);
        cart.setImg(img);

        return cart;
    }
}
